package com.cdzy.service.impl;

import com.cdzy.common.ApiException;
import com.cdzy.common.ApiExceptionCodeMsg;
import com.cdzy.mapper.UserMapper;
import com.cdzy.pojo.User;
import com.cdzy.pojo.UserVo;
import com.cdzy.service.UserService;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 用户表业务层的实现类
 */
@Service    // 把这个实现类实例化，然后加入spring容器
public class UserServiceImpl implements UserService {

    @Autowired  // 从spring容器里面取出具有UserMapper类型的实例
    UserMapper userMapper;

    @Override
    public UserVo getUserList(User user) {
        // PageHelper提供的一个方法，用于启动分页功能
        PageHelper.startPage(user.getPageNum(), user.getPageSize());

        List<User> users = userMapper.selectUserList(user);

        int total = userMapper.selectUserListCount(user);

        // 如果用户列表为空
        if (users.size() < 1) {
            throw new ApiException(ApiExceptionCodeMsg.NO_DATA_FOUND);
        }

        UserVo userVo = new UserVo();
        userVo.setUserList(users);
        userVo.setTotal(total);

        return userVo;
    }

    @Override
    public User getUserDetail(int id) {
        User user = userMapper.selectUserDetail(id);
        if (user == null) {
            throw new ApiException(ApiExceptionCodeMsg.NO_DATA_FOUND);
        }
        return user;
    }

    @Override
    public boolean addUser(User user) {
        boolean flag = false;
        Date date = new Date(); // 当前时间
        user.setCreatedAt(date);
        user.setUpdatedAt(date);
        // 插入数据
        int rows = 0;
        try {
            rows = userMapper.insertUser(user);
        } catch (Exception e) {
            throw new ApiException(ApiExceptionCodeMsg.ADD_USER_FAILED);
        }

        if (rows > 0) {
            flag = true;
        }
        return flag;
    }

    @Override
    public boolean removeUser(int id) {
        boolean flag = false;
        int rows = 0;
        try {
            rows = userMapper.deleteUser(id);
        } catch (Exception e) {
            throw new ApiException(ApiExceptionCodeMsg.DELETE_USER_FAILED);
        }
        if (rows > 0) {
            flag = true;
        }
        return flag;
    }

    @Override
    public boolean modifyUser(User user) {
        boolean flag = false;
        Date date = new Date(); // 当前时间
        user.setUpdatedAt(date);
        int rows = 0;
        try {
            rows = userMapper.updateUser(user);
        } catch (Exception e) {
            throw new ApiException(ApiExceptionCodeMsg.OPERATION_FAILED);
        }

        if (rows > 0) {
            flag = true;
        }
        return flag;
    }

    @Override
    public boolean removeUserBatch(List<Integer> idList) {
        boolean flag = false;
        int rows = 0;
        try {
            rows = userMapper.deleteUserBatch(idList);
        } catch (Exception e) {
            throw new ApiException(ApiExceptionCodeMsg.OPERATION_FAILED);
        }
        if (rows > 0) {
            flag = true;
        }
        return flag;
    }

    @Override
    public User login(User user) {
        User loginUser = userMapper.selectLogin(user);
        // 为空则代表登录失败
        if (loginUser == null) {
            throw new ApiException(ApiExceptionCodeMsg.INCORRECT_USERNAME_OR_PASSWORD);
        }
        return loginUser;
    }
}
